// GameBrowserTopPanel.cpp : implementation file
//

#include "stdafx.h"
#include "../GameBrowser.h"
#include "GameBrowserTopPanel.h"
#include "GameBrowserUtil.h"

#include "../GameBrowserDoc.h"

#include "shlwapi.h"



// CGameBrowserTopPanel

IMPLEMENT_DYNAMIC(CGameBrowserTopPanel, CWnd)

CGameBrowserTopPanel::CGameBrowserTopPanel()
{

}

CGameBrowserTopPanel::~CGameBrowserTopPanel()
{
}


BEGIN_MESSAGE_MAP(CGameBrowserTopPanel, CWnd)
	ON_WM_CREATE()
    ON_WM_SIZE()
    //ON_WM_ERASEBKGND()
	ON_MESSAGE(WM_SIZEPARENT, &CGameBrowserTopPanel::OnSizeParent)   
	ON_WM_DESTROY()
	ON_COMMAND(GM_PANEL_ADDRESS_GO_ID, &CGameBrowserTopPanel::OnBrowserUrl)		
END_MESSAGE_MAP()

BOOL CGameBrowserTopPanel::CreateTopPanel( CWnd *pParent,UINT id )
{
	CString strWndClass(::AfxRegisterWndClass(CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW,     
		LoadCursor(0,IDC_ARROW), ::GetSysColorBrush(COLOR_BTNFACE)));   

	DWORD dwStyle = WS_CHILD|WS_VISIBLE;  
	BOOL bRet = CWnd::Create(strWndClass,L"CGameBrowserTopPanel",dwStyle,CRect(0,0,0,0),pParent,id);

	return bRet;
}

BOOL CGameBrowserTopPanel::OnEraseBkgnd(CDC* pDC)
{
    return TRUE;
}

void CGameBrowserTopPanel::UpdateHistoryAddress()
{
	m_wndAddress.ResetContent();
	int nAddressCount = m_addressHistories.GetHistoryCount();
	for (int index = 0 ;index < nAddressCount; index++)
	{
		CString str = m_addressHistories.GetUrlByIndex(index);
		m_wndAddress.AddString(str);
		//COMBOBOXEXITEM item;
		//item.mask = CBEIF_TEXT;
		//item.iItem = -1;
		//item.pszText = (LPTSTR)(LPCTSTR)str;
		//m_wndAddress.InsertItem(&item);
	}
}

int CGameBrowserTopPanel::OnCreate( LPCREATESTRUCT lpCreateStruct )
{

    // create a combo box for the address bar
    if ( FALSE == m_wndAddress.Create( CBS_DROPDOWN|WS_CHILD|WS_VISIBLE, CRect(0, 0, 100, 20), this, GM_PANEL_ADDRESS_ID ) )
    {
        TRACE0("Failed to create combobox\n");
        return -1;      // fail to create
    }
	m_wndGoButton.Create(NULL,WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,CRect(0,0,50,32),this,GM_PANEL_ADDRESS_GO_ID);
	m_wndGoButton.LoadBitmaps(IDB_BMP_GO_NORMAL,IDB_BMP_GO_HOVER,IDB_BMP_GO_NORMAL,IDB_BMP_GO_UP);


	m_addressHistories.LoadHistoryUrls();
	
	UpdateHistoryAddress();

	//Create NavigateBar

	BOOL bRet = m_wndNavigateBar.CreateNavigateBar(this,GM_PANEL_NAVIGATIOINBAR_ID);

    HRESULT hr = ::SHAutoComplete(m_wndAddress.GetEditControl()->m_hWnd,SHACF_URLALL);

	return CWnd::OnCreate(lpCreateStruct);
}
void CGameBrowserTopPanel::OnSize( UINT ui, int x, int y )
{

    if (IsWindow(m_wndAddress))
    { 
        m_wndAddress.SetWindowPos(NULL,
            0, 0, 
            x - 36, 100,
            SWP_NOMOVE | SWP_NOZORDER 
            );
    }

	CRect rcAddress;
	m_wndAddress.GetWindowRect(&rcAddress);

	m_wndGoButton.SetWindowPos(NULL,x - 33,0,30,rcAddress.Height(),SWP_NOZORDER);

	m_wndNavigateBar.SetWindowPos(NULL,0,rcAddress.Height(),x,20,SWP_NOZORDER);


    CWnd::OnSize(ui,x,y);
}

LRESULT CGameBrowserTopPanel::OnSizeParent( WPARAM wParam, LPARAM lParam )
{
	AFX_SIZEPARENTPARAMS* lpLayout = (AFX_SIZEPARENTPARAMS*)lParam;

	int n_oriWidth = lpLayout->rect.right - lpLayout->rect.left;
	int n_oriHeight = lpLayout->rect.bottom - lpLayout->rect.top;

	CRect rcWinPos;

	rcWinPos.top = lpLayout->rect.top;
	rcWinPos.bottom = rcWinPos.top + GM_TOP_PANEL_HEIGHT;
	rcWinPos.left = lpLayout->rect.left;
	rcWinPos.right = lpLayout->rect.right;//lpLayout->rect.right;

	lpLayout->rect.top += GM_TOP_PANEL_HEIGHT;
	lpLayout->sizeTotal.cy -= GM_TOP_PANEL_HEIGHT;


	MoveWindow(rcWinPos.left,rcWinPos.top,rcWinPos.Width(),rcWinPos.Height(),TRUE);  


	return TRUE;    
}

// CGameBrowserTopPanel message handlers


void CGameBrowserTopPanel::OnDestroy()
{
	m_addressHistories.SaveHistoryUrls();
    m_wndNavigateBar.DestroyWindow();
	CWnd::OnDestroy();

	// TODO: Add your message handler code here
}

void  CGameBrowserTopPanel::OnUserInputEnter()
{
	OnBrowserUrl();
}

void CGameBrowserTopPanel::OnBrowserUrl()
{
	// TODO: Add your command handler code here

	CString str = m_wndAddress.GetCurrentInputURL();
	if (str.GetLength() <=0)
	{
		return;
	}
	//CDocument*pDoc = AfxGetApp()->OpenDocumentFile(str);
	POSITION pt = AfxGetApp()->m_pDocManager->GetFirstDocTemplatePosition();
	CDocTemplate *pDocumentTemplata = AfxGetApp()->m_pDocManager->GetNextDocTemplate(pt);
	CDocument *pDoc = pDocumentTemplata->OpenDocumentFile(NULL,TRUE);

	ASSERT(pDoc != NULL);
	if (pDoc != NULL)
	{
		CGameBrowserDoc *pGameDoc = (CGameBrowserDoc*)(pDoc);
		pGameDoc->CurrentUrl(str);
	}
}
